package common.util
{
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	
	import ui.tree.BaseTree;
	import ui.tree.CommonTree;

	public class TreeUtil
	{
		public function TreeUtil()
		{
		}
//********************************设置checkbox树函数****************************************************	
		/**
		 * 设置树的节点的勾选
		 * 并且更新其父子节点的勾选状态
		 * 
		 */ 
		public static function setTreeElementCheck(tree:BaseTree,checkedElements:ArrayCollection,idFiled:String,checkValue:String="1",clazz:Class=null):void{
			if(!checkedElements){
				return;
			}
			for each(var obj:Object in checkedElements){
				var treeObj:Object = getTreeObjectById(tree,obj[idFiled],clazz);
				if(!treeObj){
					continue;
				}
				treeObj.checked = checkValue;
				if (treeObj.hasOwnProperty("children")&& treeObj.children && treeObj.children.length > 0)
				{
					updateChildrenChecked(treeObj.children,checkValue);
				}
				
//				var itemParent:Object = tree.getParentItem(treeObj);
				var itemParent:Object = treeObj.parent;
				if(itemParent != null)
					updateParentChecked(tree,itemParent,checkValue);
			}
			
		
		}
		
		/**
		 * 判断自己的子中是否包含 这个模型
		 * @param hasChildObj 需要递归判断儿子集合的元素
		 * @param targetObj 需要判断的元素
		 * @parma idFeild 判断的属性名称
		 */ 
		public static function isInChild(hasChildObj:Object,targetObj:Object,idFeild:String):Boolean{
			if(hasChildObj.hasOwnProperty("children") && hasChildObj.children){
				for each(var _obj:Object in hasChildObj.children){
					if(targetObj[idFeild] == _obj[idFeild]){
						return true;
					}
					isInChild(hasChildObj,_obj,idFeild);
				}
			}
			return false;
		}
		
		private static function updateParentChecked(tree:BaseTree,parent:Object,isChecked:String):void
		{
			var allChecked:Boolean = true;
			var noneChecked:Boolean = true;
			
			var siblings:Object;
			siblings = parent.children;
			
			for each (var currentSibling:Object in siblings)
			{
				if(currentSibling.checked != "1")
				{
					allChecked = false;
				} 
				if (currentSibling.checked != "0") {
					noneChecked = false;
				}
			}
			
			var checkValue:String;
			if(allChecked){
				checkValue = "1";
			} else if (noneChecked){
				checkValue = "0";
			} else {
				checkValue = "2";
			}
			
			parent.checked = checkValue;
			
//			var itemParent:Object = tree.getParentItem(parent);
			var itemParent:Object = parent.parent;
			if(itemParent != null)
				updateParentChecked(tree,itemParent,isChecked);
		}
		
		private static function updateChildrenChecked(children:Object,isChecked:String):void
		{
			for each (var currentChild:Object in children)
			{
				currentChild.checked = isChecked;
				
				if (currentChild.hasOwnProperty("children") && currentChild.children && currentChild.children.length > 0)
				{
					updateChildrenChecked(currentChild.children,isChecked);
				}
			}
		}
		
		
		/**
		 * 设置树上勾选元素
		 * @param tree 树句柄
		 * @param checkedAc 需要勾选的对象元素集合
		 * @param idAttrName 唯一键名称，例如"id"
		 */ 
		public static function setTreeCheckedElement(tree:BaseTree,checkedElementAc:ArrayCollection,idAttrName:String):void{
			if(!checkedElementAc){
				return;
			}
			var curCheckedKeyArr:Array = DataUtil.getAllObjectKeyMapByKey(checkedElementAc,idAttrName);
			refreshTreeCheckState(tree,curCheckedKeyArr,idAttrName);
		}

		/**
		 * 根据条件刷新复选框树上的选择状态以及展开状态
		 * tree复选框树
		 * curCheckedKeyArr:该树需要被选中的集合（键值集合）
		 * attr:键属性
		 */	
		private static function refreshTreeCheckState(tree:BaseTree,curCheckedKeyArr:Array,attr:String):void{
			if(!curCheckedKeyArr){
				return;
			}
			var allArr:ArrayCollection = tree.dataProvider as ArrayCollection
			if(allArr){
				tree.validateNow();
				var lenth:int =	allArr.length;
				for(var i:int = 0; i < lenth; i++){
					refreshCheckBoxTreeChildState(allArr[i],tree,curCheckedKeyArr,attr);
				}
			}
		}
		
		//设置树展开状态  同时清掉以前的状态
		private static function refreshCheckBoxTreeChildState(obj:Object,tree:BaseTree,curCheckedKeyArr:Array,attr:String):Boolean{
			var checked:Boolean = false;
			if(obj.hasOwnProperty(attr) && curCheckedKeyArr[obj[attr]]){
				obj.checked="1";
				checked = true;
			}else{
				obj.checked="0";
			}
			var child:ArrayCollection=obj.children;
			if(child){
				for(var i:int = 0; i < child.length; i++){
					checked = refreshCheckBoxTreeChildState(child[i],tree,curCheckedKeyArr,attr) || checked;
				}
			}
			tree.expandItem(obj,checked);
			return checked;
		}
		
		/**
		 * 递归搜索树上的节点 返回指定ID的节点 并展开递归到的节点
		 */ 
		public static function getTreeObjectById(tree:BaseTree,id:String, clazz:Class = null , keyField:String = 'id'):Object{
			var array:ArrayCollection = tree.dataProvider as ArrayCollection;
			var obj:Object = null;
			if(array){
				var temp:Object = getTreeObject(array,id,clazz ,keyField);
				if(temp){
					obj = temp;
				}
			}
			return obj;
		}
		
		private static function getTreeObject(array:ArrayCollection , id:String,clazz:Class = null , keyField:String = 'id') :Object{
			var obj:Object = null;
			for each(var node:Object in array){
				if(node.hasOwnProperty(keyField)){
					if(node[keyField] == id){
						if(clazz && node is clazz){
							obj = node;
							break;
						}
						if(!clazz){
							obj = node;
							break;
						}
					}
				}
				
				if(node.hasOwnProperty("children")){
					if(node.children){
						var childrenArray:ArrayCollection = node.children as ArrayCollection;
						var temp:Object = getTreeObject(childrenArray,id,clazz,keyField);
						if(temp){
							obj = temp;
							break;
						}
					}
				}
			}
			
			return obj;
		}
		
		/**
		 * 获取复选框树种已经选中的节点
		 * tree复选框树
		 * clazz:需要获取的传入后台的模型类型,不为空则满足该类型的元素将被获取
		 * onlyTopNode:是否只获取最高节点，true是，false否
		 */
		public static function getTreeCheckedElement(tree:BaseTree,clazz:Class=null,onlyTopNode:Boolean=false):Array{
			var allArr:ArrayCollection = tree.dataProvider as ArrayCollection;
			var reustlChekcedList:Array = new Array();
			if(allArr){
				var length:int = allArr.length;
				for(var i:int = 0; i < length; i++){
					getObjCheckState(allArr[i],reustlChekcedList,onlyTopNode,clazz);
				}
			}
			return reustlChekcedList;
		}
		// --递归获取选中的节点
		private static function getObjCheckState(obj:Object,reustlChekcedList:Array,onlyTopNode:Boolean,clazz:Class=null):void{
			if(obj.hasOwnProperty("checked")){
				if(obj.checked=="1"){//选中
					if(clazz){
						if(obj is clazz){
							reustlChekcedList.push(obj as clazz);
						}
					}else{
						reustlChekcedList.push(obj);
					}
					if(onlyTopNode){//只需要保存每个枝节点的最顶层
						return;
					}
				}else if(obj.checked=="2"){//根节点接触
					if(!onlyTopNode){
						if(clazz){
							if(obj is clazz){
								reustlChekcedList.push(obj as clazz);
							}
						}else{
							reustlChekcedList.push(obj);
						}
					}
				}
			}
			if(obj.hasOwnProperty("children")){
				var child:ArrayCollection= obj.children;
				if(child){
					var lenth:int =	child.length;
					for(var i:int = 0; i < lenth; i++){
						getObjCheckState(child[i],reustlChekcedList,onlyTopNode,clazz);
					}
				}
			}
		}	
		
		/**
		 * 取消树上选中的节点
		 * 
		 */
		public static function resetTreeCheckedElement(tree:BaseTree):Array{
			var allArr:ArrayCollection = tree.dataProvider as ArrayCollection;
			var reustlChekcedList:Array = new Array();
			if(allArr){
				var length:int = allArr.length;
				for(var i:int = 0; i < length; i++){
					getCheckNode(allArr[i]);
				}
			}
			return reustlChekcedList;
		}
		
		private static function getCheckNode(obj:Object):void{
			if(obj.hasOwnProperty("checked")){
				obj.checked="0"
			}
			if(obj.hasOwnProperty("children")){
				var child:ArrayCollection= obj.children;
				if(child){
					var lenth:int =	child.length;
					for(var i:int = 0; i < lenth; i++){
						getCheckNode(child[i]);
					}
				}
			}
		}
//*****************************************************************************************
		
	}
}